package com.reebake.ideal.user.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.holder.UserContextHolder;
import com.reebake.ideal.user.entity.UserCredential;
import com.reebake.ideal.user.entity.UserInfo;
import com.reebake.ideal.user.service.UserAuthService;
import com.reebake.ideal.user.service.UserCredentialService;
import com.reebake.ideal.user.service.UserInfoService;
import com.reebake.ideal.user.vo.UserBasicInfo;
import com.reebake.ideal.user.vo.SimpleUserCredentialVO;
import com.reebake.ideal.user.vo.UserCredentialRegisterVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
@RequestMapping("/user/my")
public class MyController {
    private final UserInfoService userInfoService;
    private final UserCredentialService userCredentialService;
    private final UserAuthService userAuthService;

    @GetMapping("/info")
    public UserBasicInfo userInfo() {
        String userId = UserContextHolder.getCurrentUserId();
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getUserId, userId);
        UserInfo userInfo = userInfoService.getOne(queryWrapper);

        return BeanUtil.copyProperties(userInfo, UserBasicInfo.class);
    }

    @PostMapping("/info")
    public void updateUserInfo(@RequestBody UserBasicInfo userBasicInfo) {
        String userId = UserContextHolder.getCurrentUserId();
        UserInfo userInfo = BeanUtil.copyProperties(userBasicInfo, UserInfo.class);
        userInfo.setUserId(userId);

        userInfoService.updateById(userInfo);
    }

    @GetMapping("credential")
    public SimpleUserCredentialVO credential() {
        String userId = UserContextHolder.getCurrentUserId();
        LambdaQueryWrapper<UserCredential> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCredential::getUserId, userId);
        UserCredential userCredential = userCredentialService.getOne(queryWrapper);

        return BeanUtil.copyProperties(userCredential, SimpleUserCredentialVO.class);
    }

    @PostMapping("credential/username")
    public void changeUsername(@RequestBody UserCredentialRegisterVO userCredentialRegisterVO) {
        String userId = UserContextHolder.getCurrentUserId();
        String password = userCredentialRegisterVO.getPassword();
        String newUsername = userCredentialRegisterVO.getUsername();
        userAuthService.changeUsername(userId, password, newUsername);
    }

    @PostMapping("credential/password")
    public void changePassword(@RequestBody UserCredentialRegisterVO userCredentialRegisterVO) {
        String userId = UserContextHolder.getCurrentUserId();
        String password = userCredentialRegisterVO.getPassword();
        String newPassword = userCredentialRegisterVO.getNewPassword();
        userAuthService.changePassword(userId, password, newPassword);
    }

    @PostMapping("credential/phone-number")
    public void changePhoneNumber(@RequestBody UserCredentialRegisterVO userCredentialRegisterVO) {
        String userId = UserContextHolder.getCurrentUserId();
        String verifyCode = userCredentialRegisterVO.getVerifyCode();
        String phoneNumber = userCredentialRegisterVO.getPhoneNumber();
        String newVerifyCode = userCredentialRegisterVO.getNewVerifyCode();
        userAuthService.changePhoneNumber(userId, verifyCode, phoneNumber, newVerifyCode);
    }

    @PostMapping("delete")
    public void deleteUser(){
        String userId = UserContextHolder.getCurrentUserId();
        userAuthService.deleteUser(userId);
    }
}
